package org.springblade.modules.restaurant.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.restaurant.entity.ReportedLoss;
import org.springblade.modules.restaurant.service.IReportedLossService;
import org.springblade.modules.restaurant.vo.ReportedLossVO;
import org.springframework.web.bind.annotation.*;

/**
 * @author harry
 * @公众号 Harry技术
 */
@RestController
@AllArgsConstructor
@RequestMapping("/api/restaurant/reportedloss")
@Tag(name = "API-报损", description = "报损表接口")
public class ApiReportedLossController {

	private IReportedLossService reportedLossService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入reportedLoss")
	public R<ReportedLoss> detail(ReportedLoss reportedLoss) {
		ReportedLoss detail = reportedLossService.getOne(Condition.getQueryWrapper(reportedLoss));
		return R.data(detail);
	}

	/**
	 * 分页 报损表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入reportedLoss")
	public R<IPage<ReportedLoss>> list(ReportedLossVO reportedLoss, Query query) {
		LambdaQueryWrapper<ReportedLoss> wrapper = customizeQueryWrapper(reportedLoss);
		IPage<ReportedLoss> pages = reportedLossService.page(Condition.getPage(query), wrapper);
		return R.data(pages);
	}

	/**
	 * 新增或修改 报损表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description = "传入reportedLoss")
	public R submit(@Valid @RequestBody ReportedLoss reportedLoss) {
		return R.status(reportedLossService.saveOrUpdate(reportedLoss));
	}

	private LambdaQueryWrapper<ReportedLoss> customizeQueryWrapper(ReportedLossVO reportedLoss) {
		String[] searchTime = reportedLoss.getSearchTime();
		DateTime now = DateUtil.date();
		// 获取今天开始时间  yyyy-MM-dd
		String startTime = DateUtil.formatDate(now);
		// 获取当前时间 yyyy-MM-dd
		String endTime = DateUtil.formatDate(now);

//		if (searchTime != null && searchTime.length == 2) {
//			startTime = searchTime[0];
//			endTime = searchTime[1];
//		}
		return Wrappers.<ReportedLoss>lambdaQuery()
			.eq(ObjUtil.isNotNull(reportedLoss.getUserId()), ReportedLoss::getUserId, reportedLoss.getUserId())
			.eq(ObjUtil.isNotNull(reportedLoss.getItemType()), ReportedLoss::getItemType, reportedLoss.getItemType())
			.between(StrUtil.isNotBlank(reportedLoss.getStartTime()), ReportedLoss::getDeclareDate, startTime, endTime)
			.like(StrUtil.isNotBlank(reportedLoss.getItemName()), ReportedLoss::getItemName, reportedLoss.getItemName())
			.orderByDesc(ReportedLoss::getDeclareDate)
			.orderByDesc(ReportedLoss::getCreateTime);
	}
}
